package cn.cool.trigger.http;

import cn.cool.api.IDCCService;
import cn.cool.api.response.Response;
import cn.cool.types.common.Constants;
import cn.cool.types.enums.ResponseCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/v1/gbm/dcc/")
@RequiredArgsConstructor
public class DCCController implements IDCCService {

    private final RTopic dccTopic;


    // 降级：http://localhost:8091/api/v1/gbm/dcc/update_config?key=downgradeSwitch&value=0
    // 切量：http://localhost:8091/api/v1/gbm/dcc/update_config?key=cutRange&value=100
    @GetMapping("update_config")
    @Override
    public Response<Boolean> updateConfig(@RequestParam String key, @RequestParam String value) {
        try {
            dccTopic.publish(key + Constants.SPLIT + value);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}
